/**
 * Created by JetBrains WebStorm.
 * User: Tonis
 * Date: 20.02.12
 * Time: 17:22
 * To change this template use File | Settings | File Templates.
 */

function loadImage(name) {
    images[name] = new Image();
    images[name].onload = function () {
        resourceLoaded();
    };
    images[name].src = "img/" + name + ".png";
}

function resourceLoaded() {
    loadingReady();
    numResourcesLoaded += 1;
    if(numResourcesLoaded == totalResources) {
        contentLoaded = true;
    }
}
function getMouse(e) {
    var element = this.canvas, offsetX = 0, offsetY = 0, mx, my;

    // Compute the total offset
    if (element.offsetParent !== undefined) {
        do {
            offsetX += element.offsetLeft;
            offsetY += element.offsetTop;
        } while ((element = element.offsetParent));
    }

    mx = e.pageX - offsetX;
    my = e.pageY - offsetY;

    // We return a simple javascript object (a hash) with x and y defined
    return {x:mx, y:my};
}
function drawGridObject(spriteSheet,sx, sy, x, y) {
	try {
		ctx.drawImage(spriteSheet, sx * grid, sy * grid, grid, grid, x, y, grid, grid);
	} catch(e) {
		alert(e);
	}
}

function isPlaceFree(x, y) {
    var t = level.map[x][y];
    if (t.length > 0) {
        switch (t.charAt(0)) {
            case 'w':
                return false;
                break;
        }
    }
    return true;
}

function log(msg) {
    setTimeout(function() {
        throw new Error(msg);
    }, 0);
}